package com.chinasofti1.info_manager.dao;

import com.chinasofti1.info_manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> stus = new  ArrayList<>();
    static {
        Student stu1 = new Student("0011","lilei","13","2010-10-10");
        Student stu2 = new Student("0011","zhangsan","12","2010-10-10");
        stus.add(stu1);
        stus.add(stu2);
    }
    //添加学生
    @Override
    public boolean addStudent(Student stu) {
      stus.add(stu);
      return true;
    }
    //查询学生
    public Student[] findAllStudent() {
        Student [] students = new Student[stus.size()];
        for (int i = 0;i<students.length;i++){
            students[i]=stus.get(i);
        }
        return students;
    }
    //删除学生
    public void deleteStudentById(String id) {
        int index = getIndex(id);//查询被删除id索引的位置在不在
        stus.remove(index);//将索引位置使用null覆盖
    }
    //修改
    public void updateStudent(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus.set(index,newStu);
    }
    //查询id是否存在
    public int  getIndex(String id){
        int index= -1;
        for (int i = 0; i <stus.size() ; i++) {
            Student stu = stus.get(i);
            if(stu!=null && stu.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;

    }
}
